package org.geekbang.thinking.in.spring.bean.lifecycle;

import org.geekbang.thinking.in.spring.ioc.overview.domain.User;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;

/**
 * Bean 元信息配置示例
 */
public class BeanMetadataConfigurationDemo {

    public static void main(String[] args) {
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

        // 实例化 Properties 资源 BeanDefinitionReader
        PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(beanFactory);

        String path = "META-INF/user.properties";
        // 加载 properties 资源
        // 指定字符编码 UTF-8
        EncodedResource resource = new EncodedResource(new ClassPathResource(path), "UTF-8");
        int beanNumbers = reader.loadBeanDefinitions(resource);
        System.out.println("已加载的 BeanDefinition 数量：" + beanNumbers);

        // 通过 id和类型查找
        User user = beanFactory.getBean("user", User.class);
        System.out.println("user: " + user);
    }

}
